package com.yfqy.admin.transform;

import com.yfqy.admin.constant.MyConstants;
import com.yfqy.admin.service.SystemUserService;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class UserNameTranslator {

    // 假设有一个用户服务或DAO来获取用户信息
    private final SystemUserService userService;

    // 缓存用户信息，减少数据库查询
    private final Map<Long, String> userCache = new ConcurrentHashMap<>();

    public UserNameTranslator(SystemUserService userService) {
        this.userService = userService;
    }

    /**
     * 转换用户ID为用户名的通用方法
     *
     * @param t 需要转换的对象或集合
     */
    public void translationUser(Object t) {
        if (t == null) {
            return;
        }

        try {
            if (t instanceof List<?> list) {
                // 处理集合情况
                for (Object item : list) {
                    processSingleObject(item);
                }
            } else {
                // 处理单个对象情况
                processSingleObject(t);
            }
        } catch (Exception e) {
            throw new RuntimeException("用户信息转换失败", e);
        }

    }

    /**
     * 处理单个对象的用户ID转换
     *
     * @param obj 需要处理的对象
     */
    private void processSingleObject(Object obj) throws Exception {
        if (obj == null) {
            return;
        }

        Class<?> clazz = obj.getClass();

        // 获取createdBy字段
        Field createdByField = getField(clazz, "createdBy");
        // 获取modifiedBy字段
        Field modifiedByField = getField(clazz, "modifiedBy");
        if (createdByField == null && modifiedByField == null) {
            return;
        }
        Long createdBy = MyConstants._L1, modifiedBy = MyConstants._L1;
        if (createdByField != null) {
            createdByField.setAccessible(true);
            createdBy = (Long) createdByField.get(obj);
        }
        if (modifiedByField != null) {
            modifiedByField.setAccessible(true);
            modifiedBy = (Long) modifiedByField.get(obj);

        }
        // 如果createdBy,modifiedBy为-1，直接返回
        if (createdBy.equals(MyConstants._L1) && modifiedBy.equals(MyConstants._L1)) {
            return;
        }
        Field createdByNameField = getField(clazz, "createdByName");
        Field modifiedByNameField = getField(clazz, "modifiedByName");
        // 如果没有createdByName,modifiedByName字段，直接返回
        if (createdByNameField == null && modifiedByNameField == null) {
            return;
        }
        // 从缓存或数据库获取用户名
        String createdUserName = userCache.computeIfAbsent(createdBy, userService::getUserNameById);
        String modifiedUserName = userCache.computeIfAbsent(modifiedBy, userService::getUserNameById);

        // 设置userName字段值
        if (createdByNameField != null) {
            createdByNameField.setAccessible(true);
            createdByNameField.set(obj, createdUserName);
        }
        if (modifiedByNameField != null) {
            modifiedByNameField.setAccessible(true);
            modifiedByNameField.set(obj, modifiedUserName);
        }

    }

    /**
     * 获取类中的字段，包括父类的字段
     */
    private Field getField(Class<?> clazz, String fieldName) {
        try {
            return clazz.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            // 尝试从父类查找
            Class<?> superClass = clazz.getSuperclass();
            if (superClass != null) {
                return getField(superClass, fieldName);
            }
            return null;
        }
    }
}

